﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.Core.Modelling.Primitives;
using ITAP.ShprossesBuilder.Properties;

namespace ITAP.ShprossesBuilder.Operations
{
    class OperationStickUnstickHalfSunConnectorToShpross : IOperation
    {
        #region IOperation Members

        public string Name
        {
            get { return "Прикрепить/открепить полусолнышко от профиля"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossConnectorOperation; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.StickHalfSunConnectorToShpross.ToBitmap(); }
        }

        public void Execute(ModelControl modelControl)
        {
            Shpross shpross = null;
            ShprossConnector shprossConnector = null;
            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
            {
                if (selectedComponent is ShprossConnector)
                    shprossConnector = selectedComponent as ShprossConnector;
                if (selectedComponent is Shpross)
                    shpross = selectedComponent as Shpross;
                if (shpross != null && shprossConnector != null)
                    break;
            }

            if (shpross == null && shprossConnector != null)
            {
                if (shprossConnector.ShprossConnectorType == ShprossConnectorType.HalfSunRays)
                {
                    List<IComponent> stickConnector = shprossConnector.Stub.InnerComponents.GetComponentsByType(typeof(StickConnector));
                    if (stickConnector.Count == 1)
                        stickConnector[0].DisconnectFromAll();
                }
                return;
            }
             
            if (shpross == null || shprossConnector == null 
                || shprossConnector.ShprossConnectorType != ShprossConnectorType.HalfSunRays)
                return;
            List<IComponent> stickComponent = shprossConnector.Stub.InnerComponents.GetComponentsByType(typeof(StickConnector));
            if (stickComponent.Count == 0)
            {
                StickConnector stickConnector = ComponentFactory.CreateStickConnectorForShprossConnector(1, 1);
                stickConnector.MoveTo(shprossConnector.Position);
                stickConnector.Rotate(shprossConnector.Angle.Value + Math.PI / 2);
                shprossConnector.Stub.AddInnerComponent(stickConnector);
                ConnectionProvider.Connect(stickConnector, shprossConnector.Stub, EndType.End);
                ConnectionProvider.Connect(stickConnector, shpross, EndType.Start);
            }
            else
            {
                shprossConnector.Stub.RemoveInnerComponent(stickComponent[0]);
            }
            modelControl.UpdateModel();
        }

        public bool CanUndo
        {
            get { return true; }
        }

        #endregion
    }
}
